package com.sosolabs.com.site.client;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasWidgets;
import com.sosolabs.com.site.client.event.AdUserEvent;
import com.sosolabs.com.site.client.event.AdUserEventHandler;
import com.sosolabs.com.site.client.event.CellBrowserEvent;
import com.sosolabs.com.site.client.event.CellBrowserEventHandler;
import com.sosolabs.com.site.client.event.CellListEvent;
import com.sosolabs.com.site.client.event.CellListEventHandler;
import com.sosolabs.com.site.client.event.CellTableEvent;
import com.sosolabs.com.site.client.event.CellTableEventHandler;
import com.sosolabs.com.site.client.event.CellTreeEvent;
import com.sosolabs.com.site.client.event.CellTreeEventHandler;
import com.sosolabs.com.site.client.presenter.AdUserPresenter;
import com.sosolabs.com.site.client.presenter.CellBrowserPresenter;
import com.sosolabs.com.site.client.presenter.CellListPresenter;
import com.sosolabs.com.site.client.presenter.CellTablePresenter;
import com.sosolabs.com.site.client.presenter.CellTreePresenter;
import com.sosolabs.com.site.client.presenter.Presenter;
import com.sosolabs.com.site.client.view.AdUserView;
import com.sosolabs.com.site.client.view.CellBrowserView;
import com.sosolabs.com.site.client.view.CellListView;
import com.sosolabs.com.site.client.view.CellTableView;
import com.sosolabs.com.site.client.view.CellTreeView;


public class AppContoller implements Presenter, ValueChangeHandler<String>{
	
	private final AdUserServiceAsync rpcService;
	private final HandlerManager eventBus;
	
	private HasWidgets container;
	
	public AppContoller(AdUserServiceAsync rpcService, HandlerManager eventBus) {
		this.rpcService = rpcService;
		this.eventBus = eventBus;
		bind();
	}
	
	private void bind() {
		History.addValueChangeHandler(this);
		
		eventBus.addHandler(AdUserEvent.TYPE, new AdUserEventHandler() {
			
			@Override
			public void onAdUser(AdUserEvent event) {
				doAdUser();
			}
		});
		
		eventBus.addHandler(CellTableEvent.TYPE, new CellTableEventHandler() {
			
			@Override
			public void onCellTable(CellTableEvent event) {
				History.newItem("cell-Table");
			}
		});
		
		/*eventBus.addHandler(CellSampleEvent.TYPE, new CellSampleEventHandler() {
			
			@Override
			public void onCellSample(CellSampleEvent event) {
				History.newItem("cell-Sample");
			}
		});*/
		
		eventBus.addHandler(CellListEvent.TYPE, new CellListEventHandler() {
			
			@Override
			public void onCellList(CellListEvent event) {
				History.newItem("cell-List");
			}
		});
		
		eventBus.addHandler(CellTreeEvent.TYPE, new CellTreeEventHandler() {
			
			@Override
			public void onCellTree(CellTreeEvent event) {
				History.newItem("cell-Tree");
			}
		});
		
		eventBus.addHandler(CellBrowserEvent.TYPE, new CellBrowserEventHandler() {
			
			@Override
			public void onCellBrowser(CellBrowserEvent event) {
				History.newItem("cell-Browser");
			}
		});
	}
	
	private void doAdUser() {
		History.newItem("add");
	}

	@Override
	public void go(HasWidgets container) {
		this.container = container;
		if("".equals(History.getToken())) {
			History.newItem("add");
		} else {
			History.fireCurrentHistoryState();
		}
		
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
String token = event.getValue();
		
		if(token!=null) {
			Presenter presenter = null;
			
			if(token.equals("add")) {
				presenter = new AdUserPresenter(rpcService, eventBus, new AdUserView());
			}
			
			if(token.equals("cell-Table")) {
				presenter = new CellTablePresenter(rpcService, eventBus, new CellTableView());
			}

			
			if(token.equals("cell-List")) {
				presenter = new CellListPresenter(rpcService, eventBus, new CellListView());
			}
			
			if(token.equals("cell-Tree")) {
				presenter = new CellTreePresenter(rpcService, eventBus, new CellTreeView());
			}
			
			if(token.equals("cell-Browser")) {
				presenter = new CellBrowserPresenter(rpcService, eventBus, new CellBrowserView());
			}
			
			if(presenter != null) {
				presenter.go(container);
			}
		}
	}
	

}
